class Page {
  constructor(pageSize = 4){
    this.pageSize = pageSize
    this.lastPage = true
    this.pageIndex = 1
    this.pages = []
    this.pageItems = []
  }
  // 上一页
  prevPage (tags) {
    if (this.pageIndex <= 1) {
      return;
    }
    this.pageIndex = this.pageIndex - 1;
    this.drawPage(tags);
  }
  // 下一页
  nextPage (tags) {
    if (
      this.pages.length > 1 &&
      this.pages[this.pages.length - 1] == this.pageIndex
    ) {
      return;
    }
    this.pageIndex = this.pageIndex + 1;
    this.drawPage(tags);
  }
  // 计算分页
  drawPage (tags) {
    var curIndex = (this.pageIndex - 1) * this.pageSize,
      sliceEnd = curIndex + this.pageSize,
      sliceItems = tags.data.list.slice(
        curIndex,
        sliceEnd
      );
    sliceItems.forEach((item, index) => {
      if (!item.width) {
        sliceItems[index].width = tags.total ? (item.signCount / tags.total * 100) : 0;
      }
    });
    var lastPageIndex =
      this.pages.length > 0 ? (this.pages[this.pages.length - 1]) : 1;

    if (this.pageIndex >= lastPageIndex) {
      this.lastPage = true;
    } else {
      this.lastPage = false;
    }
    this.pageItems = [].concat(sliceItems);
  }
  calcPages(len){
    var count = Math.ceil(len / this.pageSize);
    for (var i = 1; i <= count; i++) {
      this.pages.push(i);
    }
  }
}

export default Page
